Ext.namespace('Ext.ux.plugin');

Ext.apply(Ext.util.Format, {
	phoneNumber: function(value, a, b, c) {
		var phoneNumber = value.replace(/\./g, "").replace(/-/g, "").replace(/[^0-9]/g, "");
		if(!Ext.isEmpty(phoneNumber) && phoneNumber.length == 8) {
			return phoneNumber.substr(0, 4) + "-" + phoneNumber.substr(4, 4);
		}
		return phoneNumber;
	}
});

Ext.ux.plugin.PhoneNumber = Ext.extend(Ext.form.TextField, {
	init: function(config) {
		config.on('change', this.onChange, this);
		config.on('focus', this.onFocus, this);
		config.on('blur', this.onBeforeBlur, this);
	},
	onChange: function(field, a, b, c, d) {
		//field.setValue(Ext.util.Format.phoneNumber(field.getValue()));
		console.log(a);
		console.log(b);
		console.log(c);
		console.log(d);
	},
	onFocus: function(field, a, b, c) {
		Ext.select("#" + field.id + " input").set({maxLength: 8})
		field.maxLength = 8;
		if(!Ext.isEmpty(field.getValue())){
			field.setValue(field.getValue().replace(/\./g, "").replace(/-/g, "").replace(/[^0-9]/g, ""));
		}
		
	},
	onBeforeBlur: function(field){
		Ext.select("#" + field.id + " input").set({maxLength: 9})
		field.maxLength = 9;
		field.setValue(Ext.util.Format.phoneNumber(field.getValue()));
	}
});